package com.hk.huakangcompany.config;

import com.hk.huakangcompany.handler.MyAccessDenidHandler;
import com.hk.huakangcompany.handler.RestAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/21 18:54
 * @Statement: Security配置类
 */
@EnableGlobalMethodSecurity(prePostEnabled = true) // 会开启相关注解
@Configuration
@EnableWebSecurity //启动Spring Securiry
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private MyAccessDenidHandler myAccessDenidHandler;

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;

    /**
     * 自定义登录页面
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //设置不拦截页面，可直接通过，路径访问 "/", "/index", "/home" 则不拦截
        http.authorizeRequests()
                //静态资源和一些所有人都能访问的请求
                .antMatchers("/css/**","/lib/**","/static/**", "/js/**","/images/**","/img/**").permitAll()
                .antMatchers("/","/s/test","/admin/tologin","/admin/destroyLogin","/qiantai/**").permitAll()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .anyRequest().authenticated()// 所有请求必须登录后才可以访问
                .and()
                .userDetailsService(userDetailsService)
//               调整为让 Spring Security 不创建和使用 session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

//        关闭csrf防护 防止使用自定义登录请求不进入UserDetailsService的loadUserByUsername(String username)方法
        http.csrf().disable();

//        登录拦截过滤器token
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

//        异常处理403 401
        http.exceptionHandling()
                .accessDeniedHandler(myAccessDenidHandler)
                .authenticationEntryPoint(restAuthenticationEntryPoint);

    }

    /**
     * 注入加密
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}
